'use strict';

const BaseModel = require('./BaseModel');

/**
 * 用户角色
 */
class AdminUserRoleRelModel extends BaseModel {

  constructor() {
    super('admin_user_role_rel', 'admin_user_id');

    this.entity = class entity {
      constructor() {
        this.adminUserId = null;
        this.roleId = null;
        this.creator = null;
        this.createTime = null;
        this.updateTime = null;
      }
    };
  }

  async updateByUid(app, adminRole) {
    const sqls = [];
    sqls.push(' update admin_user_role_rel ');
    sqls.push(` set role_id = ${adminRole.roleId} `);
    sqls.push(` , update_time = '${adminRole.updateTime}' `);
    sqls.push(` where admin_user_id = '${adminRole.adminUserId}' `);
    const sql = sqls.join('');
    app.logger.debug('SQL:', sql);
    return await app.mysql.query(sql);
  }

  async updateByUid_transaction(conn, adminRole) {
    const sqls = [];
    sqls.push(' update admin_user_role_rel ');
    sqls.push(` set role_id = ${adminRole.roleId} `);
    sqls.push(` , update_time = '${adminRole.updateTime}' `);
    sqls.push(` where admin_user_id = '${adminRole.adminUserId}' `);
    const sql = sqls.join('');
    return await conn.query(sql);
  }

  async deleteByUid_transaction(conn, adminRole) {
    const sql = `delete from ${this.tableName} where admin_user_id = '${adminRole.adminUserId}' `;
    return await conn.query(sql);
  }

}

module.exports = AdminUserRoleRelModel;

